package com.hzpd.hainan.module.news;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.hzpd.hainan.R;
import com.hzpd.hainan.app.BaseFragment;
import com.hzpd.hainan.model.event.ChannelSortedList;
import com.hzpd.hainan.model.news.NewsChannelBean;
import com.hzpd.hainan.module.news.adapter.NewsFragmentPagerAdapter;
import com.hzpd.hainan.module.news.fragment.NewsItemFragment;
import com.hzpd.hainan.module.news.fragment.NewsItemFragmentNew;
import com.hzpd.hainan.utils.LogUtils;
import com.hzpd.hainan.utils.PageCtrl;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;

/**
 * Created by 猪猪侠 on 2017/7/3.
 */

public class NewsFragmentNew extends BaseFragment {
    @BindView(R.id.news_tablepage_ll)
    RelativeLayout newsTablepageLl;
    @BindView(R.id.news_pager)
    ViewPager newsPager;
    Unbinder unbinder;

    @BindView(R.id.tablayout_id)
    TabLayout tablayoutId;
    private NewsFragmentPagerAdapter adapter;

    @Override
    public int setMyContentView() {
        return R.layout.fragment_news_main_new;
    }


    public static NewsFragmentNew newInstance() {
        NewsFragmentNew newsFragment = new NewsFragmentNew();
        return newsFragment;
    }


    @OnClick(R.id.news_button)
    public void onViewClicked() {
        PageCtrl.start2EditColumnActivity(activity);
    }

    @Override
    public void initView() {
        EventBus.getDefault().register(this);
    }

    @Override
    public void initData() {
        List<NewsChannelBean> mList = DataSupport.where("ischoice=?", "1").order("sortOrder").find(NewsChannelBean.class);
        for (NewsChannelBean newsChannelBean : mList) {
            LogUtils.e("list---"+newsChannelBean.getSortOrder());
        }
        if (null == mList) {
            mList = new ArrayList<NewsChannelBean>();
        }
        LogUtils.i("mlist_size-->" + mList.size());
        List<NewsItemFragmentNew> newsItemFragmentList = new ArrayList<>();
        for (int i = 0; i < mList.size(); i++) {
            NewsChannelBean ncb = mList.get(i);
            NewsItemFragmentNew itemFragment = NewsItemFragmentNew.newInstance(i, ncb);
            itemFragment.setTitle(ncb.getCnname());
            newsItemFragmentList.add(itemFragment);

        }
        if (newsItemFragmentList.size() > 0) {
            newsTablepageLl.setVisibility(View.VISIBLE);
        }
        adapter = new NewsFragmentPagerAdapter(fm);
        newsPager.setAdapter(adapter);
        adapter.setFragments(newsItemFragmentList);
        newsPager.setOffscreenPageLimit(adapter.getCount());
        newsPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                LogUtils.i("addOnPageChangeListener--"+position);
                tablayoutId.getTabAt(position).select();

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //1
        tablayoutId.setTabMode(TabLayout.MODE_SCROLLABLE);

        //2.MODE_FIXED模式
//        tablayoutId.setTabMode(TabLayout.MODE_FIXED);
        tablayoutId.setupWithViewPager(newsPager);
        tablayoutId.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                NewsItemFragmentNew frag = (NewsItemFragmentNew) adapter.getItem(tab.getPosition());
                frag.init();
                adapter.setSelectedPosition(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Subscribe
    public void onEventMainThread(ChannelSortedList csl) {
        adapter.sortChannel(csl.getSaveTitleList());
        newsPager.setOffscreenPageLimit(adapter.getCount());
        adapter.notifyDataSetChanged();
        tablayoutId.notify();
    }
}
